import numpy as np
import getfile
import re
import paramiko
import os
# from socket import *
# true=True

def __get_all_files_in_local_dir(local_dir):
        # 保存所有文件的列表
        all_files = list()
        # 获取当前指定目录下的所有目录及文件，包含属性值
        files = os.listdir(local_dir)
        for x in files:
            # local_dir目录中每一个文件或目录的完整路径
            filename = os.path.join(local_dir, x)
            # 如果是目录，则递归处理该目录
            if os.path.isdir(x):
                all_files.extend(__get_all_files_in_local_dir(filename))
            else:
                all_files.append(filename)
        return all_files

def sftp_put_dir(local_dir, remote_dir):
        t = paramiko.Transport(sock=("192.168.1.108",22))
        t.connect(username="root", password="Admin@9000")
        sftp = paramiko.SFTPClient.from_transport(t)
        # 去掉路径字符串最后的字符'/',如果有的话
        if remote_dir[-1] == '/':
            remote_dir = remote_dir[0:-1]
        # 获取本地指定目录及其子目录下的所有文件
        all_files = __get_all_files_in_local_dir(local_dir)
        # 依次put每个文件
        for x in all_files:
            filename = os.path.split(x)[-1]
            remote_filename = remote_dir+'/'+filename
            print(u'Put文件%s传输中...' % filename)
            sftp.put(x, remote_filename)
        print('put文件传输完成')

if __name__ == '__main__':
    #socket建立连接

    # HOST = ''
    # POST = 5555
    # ADDR = (HOST, POST)
    # tcp = socket(AF_INET, SOCK_STREAM)
    # tcp.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    # tcp.bind(ADDR)
    # tcp.listen(5)
    # tcpCli, addr = tcp.accept()
    # print("连接已建立")
    # tcpCli.send('server ok'.encode('utf8'))
    # while true:
        # data = tcpCli.recv(1024).decode('utf8')
        # BIOS = np.random.randint(0,2,[1,64]).astype(float)
        BIOS=   [0, 0, 0, 1, 0, 0, 0, 1,  
                1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 
                0, 1, 1, 0, 0, 1, 1, 1]
                
          
        #0111000011010101110111101011010001000010011111011100001101011101
        # BIOS1=list(BIOS)
        # if data == "":
        #     break
        # if data == "exit":
        #     break
        # data1=data.replace('set','')
        # BIOS=data1.split(',')
        print("当前BIOS配置为：",BIOS)
        # remote_path1 = r'/home/yuytest/newhexbios'
        # local_path1 = r'E:\20201113\newhexbios'
        # sftp_put_dir(local_path1,remote_path1)
        # recordlog.logwrite().write("初始BIOS是："+str(BIOS)+'\n')
        # recordlog.logwrite().close()
        #getfile.sim(BIOS,tcpCli)
        goal=getfile.sim(BIOS)
        print("result：",goal)
        # G = goal[0]
        # print("初始化状态为：",G)
        # recordlog.logwrite().write("初始化状态为："+str(G)+'\n')
        # recordlog.logwrite().close()
        # data={'23333','23333','success','success','stream'}
        # data1= ''.join(str(i) for i in data)
        # tcpCli.send(data1.encode('utf8'))
    #tcpCli.close()
    #print('断开连接')
    #exit
    